package component.fy.com.common_module.utils;

import android.util.Base64;

import java.io.UnsupportedEncodingException;
import java.security.KeyPair;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

/**
 * 加密工具类
 */
public class EncryptUtils {
    private static SecureRandom random;
    private static KeyPair keyPair;
    /**
     * MD5
     */
    public static String getMD5(String info) {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(info.getBytes("UTF-8"));
            byte[] encryption = md5.digest();

            StringBuffer strBuf = new StringBuffer();
            for (int i = 0; i < encryption.length; i++) {
                if (Integer.toHexString(0xff & encryption[i]).length() == 1) {
                    strBuf.append("0").append(Integer.toHexString(0xff & encryption[i]));
                } else {
                    strBuf.append(Integer.toHexString(0xff & encryption[i]));
                }
            }
            return strBuf.toString();
        } catch (NoSuchAlgorithmException e) {
            return "";
        } catch (UnsupportedEncodingException e) {
            return "";
        }
    }

    /**
     * 功能简述: 使用BASE64进行加密.
     * @param plainData 明文数据
     * @return 加密之后的文本内容
     */
    public static String encryptBASE64(byte[] plainData) {
        return Base64.encodeToString(plainData, Base64.DEFAULT);
    }

    /**
     * 功能简述: 使用BASE64进行解密.
     * @param cipherText 密文文本
     * @return 解密之后的数据
     */
    public static byte[] decryptBASE64(String cipherText) {
        byte[] plainData = null;
        plainData =  Base64.decode(cipherText, Base64.DEFAULT);
        return plainData;
    }
}
